package com.example.demo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.google.gson.Gson;

public class MainActivity extends AppCompatActivity {

    private final String TAG = "MainActivity";
    private String jsonStr = "{\"name\":\"sunny\",\"age\":24}";

    private String jsonStr2 ="{\n" +
            "\t\"animals\":{\n" +
            "\t\"dog\":[\n" +
            "\t\t{\"name\":\"Rufus\",\"breed\":\"labrador\",\"count\":1,\"twoFeet\":false},\n" +
            "\t\t{\"name\":\"Marty\",\"breed\":\"whippet\",\"count\":1,\"twoFeet\":false}\n" +
            "\t],\n" +
            "\t\"cat\":{\"name\":\"Matilda\"}\n" +
            "}\n" +
            "}";
    private String jsonWeather ="{\"time\":\"2019-03-25 09:00:00\",\"cityInfo\":{\"city\":\"广州市\",\"cityId\":\"101280101\",\"parent\":\"广东\",\"updateTime\":\"08:39\"},\"date\":\"20190325\",\"message\":\"Success !\",\"status\":200,\"data\":{\"shidu\":\"99%\",\"pm25\":5.0,\"pm10\":6.0,\"quality\":\"优\",\"wendu\":\"13\",\"ganmao\":\"各类人群可自由活动\",\"yesterday\":{\"date\":\"24\",\"sunrise\":\"06:29\",\"high\":\"高温 17.0℃\",\"low\":\"低温 12.0℃\",\"sunset\":\"18:39\",\"aqi\":15.0,\"ymd\":\"2019-03-24\",\"week\":\"星期日\",\"fx\":\"无持续风向\",\"fl\":\"<3级\",\"type\":\"中雨\",\"notice\":\"记得随身携带雨伞哦\"},\"forecast\":[{\"date\":\"25\",\"sunrise\":\"06:28\",\"high\":\"高温 19.0℃\",\"low\":\"低温 14.0℃\",\"sunset\":\"18:40\",\"aqi\":18.0,\"ymd\":\"2019-03-25\",\"week\":\"星期一\",\"fx\":\"无持续风向\",\"fl\":\"<3级\",\"type\":\"小雨\",\"notice\":\"雨虽小，注意保暖别感冒\"},{\"date\":\"26\",\"sunrise\":\"06:27\",\"high\":\"高温 24.0℃\",\"low\":\"低温 16.0℃\",\"sunset\":\"18:40\",\"aqi\":69.0,\"ymd\":\"2019-03-26\",\"week\":\"星期二\",\"fx\":\"无持续风向\",\"fl\":\"<3级\",\"type\":\"多云\",\"notice\":\"阴晴之间，谨防紫外线侵扰\"},{\"date\":\"27\",\"sunrise\":\"06:26\",\"high\":\"高温 26.0℃\",\"low\":\"低温 20.0℃\",\"sunset\":\"18:41\",\"aqi\":52.0,\"ymd\":\"2019-03-27\",\"week\":\"星期三\",\"fx\":\"无持续风向\",\"fl\":\"<3级\",\"type\":\"多云\",\"notice\":\"阴晴之间，谨防紫外线侵扰\"},{\"date\":\"28\",\"sunrise\":\"06:25\",\"high\":\"高温 27.0℃\",\"low\":\"低温 20.0℃\",\"sunset\":\"18:41\",\"aqi\":55.0,\"ymd\":\"2019-03-28\",\"week\":\"星期四\",\"fx\":\"无持续风向\",\"fl\":\"<3级\",\"type\":\"多云\",\"notice\":\"阴晴之间，谨防紫外线侵扰\"},{\"date\":\"29\",\"sunrise\":\"06:24\",\"high\":\"高温 25.0℃\",\"low\":\"低温 18.0℃\",\"sunset\":\"18:41\",\"aqi\":52.0,\"ymd\":\"2019-03-29\",\"week\":\"星期五\",\"fx\":\"无持续风向\",\"fl\":\"<3级\",\"type\":\"小雨\",\"notice\":\"雨虽小，注意保暖别感冒\"},{\"date\":\"30\",\"sunrise\":\"06:23\",\"high\":\"高温 25.0℃\",\"low\":\"低温 19.0℃\",\"sunset\":\"18:42\",\"aqi\":57.0,\"ymd\":\"2019-03-30\",\"week\":\"星期六\",\"fx\":\"无持续风向\",\"fl\":\"<3级\",\"type\":\"多云\",\"notice\":\"阴晴之间，谨防紫外线侵扰\"},{\"date\":\"31\",\"sunrise\":\"06:22\",\"high\":\"高温 24.0℃\",\"low\":\"低温 18.0℃\",\"sunset\":\"18:42\",\"ymd\":\"2019-03-31\",\"week\":\"星期日\",\"fx\":\"无持续风向\",\"fl\":\"<3级\",\"type\":\"多云\",\"notice\":\"阴晴之间，谨防紫外线侵扰\"},{\"date\":\"01\",\"sunrise\":\"06:21\",\"high\":\"高温 26.0℃\",\"low\":\"低温 18.0℃\",\"sunset\":\"18:42\",\"ymd\":\"2019-04-01\",\"week\":\"星期一\",\"fx\":\"东北风\",\"fl\":\"<3级\",\"type\":\"多云\",\"notice\":\"阴晴之间，谨防紫外线侵扰\"},{\"date\":\"02\",\"sunrise\":\"06:20\",\"high\":\"高温 26.0℃\",\"low\":\"低温 18.0℃\",\"sunset\":\"18:43\",\"ymd\":\"2019-04-02\",\"week\":\"星期二\",\"fx\":\"东南风\",\"fl\":\"<3级\",\"type\":\"晴\",\"notice\":\"愿你拥有比阳光明媚的心情\"},{\"date\":\"03\",\"sunrise\":\"06:19\",\"high\":\"高温 27.0℃\",\"low\":\"低温 20.0℃\",\"sunset\":\"18:43\",\"ymd\":\"2019-04-03\",\"week\":\"星期三\",\"fx\":\"南风\",\"fl\":\"<3级\",\"type\":\"小雨\",\"notice\":\"雨虽小，注意保暖别感冒\"},{\"date\":\"04\",\"sunrise\":\"06:18\",\"high\":\"高温 30.0℃\",\"low\":\"低温 22.0℃\",\"sunset\":\"18:43\",\"ymd\":\"2019-04-04\",\"week\":\"星期四\",\"fx\":\"南风\",\"fl\":\"<3级\",\"type\":\"多云\",\"notice\":\"阴晴之间，谨防紫外线侵扰\"},{\"date\":\"05\",\"sunrise\":\"06:18\",\"high\":\"高温 31.0℃\",\"low\":\"低温 22.0℃\",\"sunset\":\"18:44\",\"ymd\":\"2019-04-05\",\"week\":\"星期五\",\"fx\":\"南风\",\"fl\":\"<3级\",\"type\":\"多云\",\"notice\":\"阴晴之间，谨防紫外线侵扰\"},{\"date\":\"06\",\"sunrise\":\"06:17\",\"high\":\"高温 30.0℃\",\"low\":\"低温 22.0℃\",\"sunset\":\"18:44\",\"ymd\":\"2019-04-06\",\"week\":\"星期六\",\"fx\":\"南风\",\"fl\":\"<3级\",\"type\":\"多云\",\"notice\":\"阴晴之间，谨防紫外线侵扰\"},{\"date\":\"07\",\"sunrise\":\"06:16\",\"high\":\"高温 29.0℃\",\"low\":\"低温 23.0℃\",\"sunset\":\"18:45\",\"ymd\":\"2019-04-07\",\"week\":\"星期日\",\"fx\":\"东南风\",\"fl\":\"<3级\",\"type\":\"小雨\",\"notice\":\"雨虽小，注意保暖别感冒\"},{\"date\":\"08\",\"sunrise\":\"06:15\",\"high\":\"高温 29.0℃\",\"low\":\"低温 23.0℃\",\"sunset\":\"18:45\",\"ymd\":\"2019-04-08\",\"week\":\"星期一\",\"fx\":\"东南风\",\"fl\":\"<3级\",\"type\":\"小雨\",\"notice\":\"雨虽小，注意保暖别感冒\"}]}}";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Gson gson = new Gson();
                String jsonNum = gson.toJson(100);
                Log.e(TAG,jsonNum);
                //生成JSON
                User user = new User("潘家锐",18);
                String gsonName = gson.toJson(user);
                Log.e(TAG,gsonName);

                //解析JSON
                User user2 = gson.fromJson(jsonStr,User.class);
                Log.e(TAG, String.valueOf(user2));

                //解析猫狗
                 Root root = gson.fromJson(jsonStr2,Root.class);
                 String catName = root.getAnimals().getCat().getName();
                 String dogName = root.getAnimals().getDog().get(1).getName();
                Log.e(TAG, "catName  "+catName +" , dogName "+dogName );
            }
        });


        findViewById(R.id.btn3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Gson gson = new Gson();
                Weather weather = gson.fromJson(jsonWeather,Weather.class);
                String city = weather.getCityInfo().getCity();

                Log.e(TAG,city);
            }
        });
    }
}
